<div class="filter">
    <div class="f8 item" style="line-height: 30px; text-align: right; width: 50px;">类型</div>
    <div class="f9 item">
        <select data-am-selected>
            <option value="sign-up" selected>注册黑名单</option>
            <option value="sign-in">登录黑名单</option>
        </select>
    </div>

    <div class="button item">
        <button type="button" class="am-btn am-btn-primary">查看</button>
    </div>
    <div class="button2 item">
        <button type="button" class="am-btn am-btn-primary">添加</button>
    </div>
    <div class="clear"></div>
</div>

<table class="am-table am-table-bordered am-table-striped am-table-compact" id="result-table">
  <thead>
  <tr>
    <th>IP</th>
    <th>IP信息</th>
    <th>操作人</th>
    <th>添加时间</th>
    <th>备注</th>
    <th>操作</th>
  </tr>
  </thead>
  <tbody>
  </tbody>
</table>
<div class="zxf_pagediv"></div>

<style type="text/css">
    #result-table td { text-align: center; }
</style>

<script>
    Util.restorePageFilter();
    var _f = Util.queryTemplate($("#result-table tbody"), ".zxf_pagediv", $(".button button"), function(page, size, cb) {
        var type = $(".f9 select").val();
        Util.savePageFilter([".f9 select"]);
        startLoading();
        CMD.blacklist_query(type, page, size, function(r) {
            stopLoading();
            cb(r);
        });
    }, function(table, list, sum) {
        if (Util.isEmpty(list)) {
            table.append('<tr><td colspan="6">没有数据</td></tr>');
            return;
        }

        var ips = [];
        for (var i in list) {
            var d = list[i];
            var memo = {};
            if (d.memo) memo = JSON.parse(d.memo);
            var tr = $("<tr></tr>");
            tr.append('<td>' + d.ip + '</td>');
            tr.append('<td class="ip">' + d.ip + '</td>');
            tr.append('<td>' + (memo.oper || '') + '</td>');
            tr.append('<td>' + (memo.time || '') + '</td>');
            tr.append('<td>' + (memo.memo || '') + '</td>');
            tr.append('<td><button value="__v__">删除</button></td>'.replace(/__v__/, list[i].id));
            table.append(tr);

            if (list[i].ip && ips.indexOf(list[i].ip) == -1) ips.push(list[i].ip);
        }
        $("#result-table button").click(function(e) {
            var v = $(e.currentTarget).attr("value");
            CMD.blacklist_delete(v, function() {
                queryBlackList(Page.currPage);
            });
        })
        for (var i in ips) registerIp(ips[i]);
    });
    
    function registerIp(ip) {
        CMD.ip_info(ip, function(r) {
            var arr = $("td.ip");
            for (var i = 0; i < arr.length; i++) {
                if ($(arr[i]).html() == ip) {
                    $(arr[i]).html(r.addr);
                }
            }
        });
    }

    $(".button2 button").click(function() {
        parent._f = _f;
        Util.addPopUp("add_ip", "add_ip", 300);
    });
</script>
